Imports Microsoft.VisualStudio.DebuggerVisualizers
Imports System.Runtime.Serialization.Formatters.Binary

Public Class StringVisualizerObjectSource
    Inherits VisualizerObjectSource

    Private Shared _objBinaryFormatter As New BinaryFormatter


#Region " Overriden Methods "

    Public Overrides Sub GetData(ByVal target As Object, ByVal outgoingData As System.IO.Stream)
        StringVisualizerObjectSource.Serialize(outgoingData, target)

        'the purpose of this function is to allow the developer to write custom code for packing up the data.
        '
        'for example, if your target object is not serializeable, then you can package the data in a serializeable format.
    End Sub

    'if you need to use these, then uncomment them
    '
    'Public Overrides Function CreateReplacementObject(ByVal target As Object, ByVal incomingData As System.IO.Stream) As Object
    '    Return MyBase.CreateReplacementObject(target, incomingData)
    'End Function
    'Public Overrides Sub TransferData(ByVal target As Object, ByVal incomingData As System.IO.Stream, ByVal outgoingData As System.IO.Stream)
    '    MyBase.TransferData(target, incomingData, outgoingData)
    'End Sub
#End Region

#Region " Helper Methods "

    ''' <summary>
    ''' This function makes debugging a Visualizer a snap.  
    ''' 1.  The project used to start this Visualizer will need to reference Microsoft.VisualStudio.DebuggerVisualizers (VS2005 use version 8) (VS2008 use version 9)
    ''' 2.  The project used to start this Visualizer will need a reference to this project.  Ensure that you set Copy Local to True.
    ''' 3.  Set desired breakpoints inside your Visualizer
    ''' 4.  Call this method from another project
    ''' 5.  Please see the following post if you have difficulties during debugging:
    '''     http://karlshifflett.wordpress.com/2007/12/01/systeminvalidcastexception-unable-to-cast-object-of-type-x-to-type-x/
    ''' </summary>
    Public Shared Sub DebugVisualizer(ByVal obj As Object)
        '
        'TODO DEVELOPERS YOU SHOULD REMOVE THIS ENTIRE METHOD FROM ANY PRODUCTION VISUALIZER
        '
        Dim vdh As VisualizerDevelopmentHost =
            New VisualizerDevelopmentHost(obj, GetType(SampleStringVisualizer.StringVisualizer), GetType(SampleStringVisualizer.StringVisualizerObjectSource))
        vdh.ShowVisualizer()
    End Sub

    ''' <summary>
    ''' Uses binary formatter to deserialize the incoming data
    ''' </summary>
    Public Overloads Shared Function Deserialize(ByVal incomingData As System.IO.Stream) As Object
        Return _objBinaryFormatter.Deserialize(incomingData)
    End Function

    ''' <summary>
    ''' Uses binary formatter to serialize the serializationStream data
    ''' </summary>
    Public Overloads Shared Sub Serialize(ByVal serializationStream As System.IO.Stream, ByVal target As Object)
        _objBinaryFormatter.Serialize(serializationStream, target)
    End Sub

#End Region

End Class
